package com.aidong.pay.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.pay.biz.TrnRefundApplyBiz;
import com.aidong.pay.entity.TrnRefundApply;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json")
@Namespace("/")
public class TrnRefundApplyAction implements ModelDriven<TrnRefundApply>{

	private TrnRefundApply trnRefundApply;
	@Override
	public TrnRefundApply getModel() {
		// TODO Auto-generated method stub
		if (trnRefundApply == null) {
			this.trnRefundApply = new TrnRefundApply();
		}
		return trnRefundApply;
	}
	
	@Autowired
	private TrnRefundApplyBiz trnRefundApplyBiz;
	public void setTrnRefundApplyBiz(TrnRefundApplyBiz trnRefundApplyBiz) {
		this.trnRefundApplyBiz = trnRefundApplyBiz;
	}
	
	Map<String, Object> map;
	@JSON
	public Map<String, Object> getMap() {
		return map;
	}
	
	/**
	 * 新增退款申请
	 * @return
	 */
	@Action(value="refund",results={@Result(name="succ",type="json",params={"root","map"})})
	public String addRefundApply() {
		int orderId = trnRefundApply.getOrderId();
		double amount = trnRefundApply.getAmount();
		String refundReason = trnRefundApply.getRefundReason();
		String addReason = trnRefundApply.getAddReason();
		
		if (orderId != 0 && amount != 0.00 && !"".equals(refundReason)) {
			map = new HashMap<String, Object>();
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("orderId", orderId);
			paraMap.put("amount", amount);
			paraMap.put("refundReason", refundReason);
			paraMap.put("addReason", addReason);
			try {
				trnRefundApplyBiz.addRefundApply(paraMap);//新增到退款申请表
				//将状态修改为退款中
				trnRefundApplyBiz.editorderRefunding(orderId);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
			
		}else {
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
		map.put("code", 1);
		map.put("result", "退款申请已提交");
		return "succ";
	}
}
